<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showCateDialog">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <tree-table
        class="tree-table"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        :show-index="true"
        :show-row-hover="false"
        index-text="#"
        border
      >
        <template slot="isOk" slot-scope="scope">
          <i
            style="color: lightgreen"
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i style="color: red" class="el-icon-error" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" v-else>三级</el-tag>
        </template>
        <template slot="opt">
          <!-- 编辑按钮 -->
          <el-button type="primary" size="mini" icon="el-icon-edit"
            >编辑</el-button
          >
          <!-- 删除按钮 -->
          <el-button type="danger" size="mini" icon="el-icon-delete"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页区域  -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[3, 5, 10, 15]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      @close="addCateClose"
      title="添加分类"
      :visible.sync="adddialogVisible"
      width="50%"
    >
      <!-- 添加分类表单 -->
      <el-form
        ref="addCateFef"
        label-width="100px"
        :model="addCateForm"
        :rules="addCateFormRules"
      >
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            clearable
            v-model="selectKeys"
            :options="partentCateList"
            :props="cascaderprops"
            @change="partentCateChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryinfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0,
      },
      selectKeys: [],
      cascaderprops: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: 'true',
      },
      partentCateList: [],
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
        ],
      },
      adddialogVisible: false,
      cateList: [],
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk',
        },
        {
          label: '排序',
          type: 'template',
          template: 'order',
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
        },
      ],
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: this.queryinfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      }

      this.cateList = res.data.result
      console.log(this.cateList)
      this.total = res.data.total
      //   this.$message.error('获取成功')
    },
    // 分页条目数改变事件
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`)
      // 改变当前每页显示条目数
      this.queryinfo.pagesize = val
      //   重新调用getCateList()获取数据,刷新页面
      this.getCateList()
    }, // 分页页码值改变事件
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`)
      // 改变当前显示的页码值
      this.queryinfo.pagenum = val
      //   重新调用getCateList()获取数据,刷新页面
      this.getCateList()
    }, // 添加分类对话框
    showCateDialog() {
      // 获取父级分类的数据列表
      this.getPartentCateList()
      this.adddialogVisible = true
    }, // 获取父级分类的数据列表方法
    async getPartentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 },
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取父级分类数据失败')
      }
      this.partentCateList = res.data
      console.log(this.partentCateList)
    }, // 父级分类change事件
    partentCateChange() {
      console.log(this.selectKeys)
      // 如果selectkeys的长度大于0，说明所选的值存在父级id
      // 反之，说明没有选定父级分类
      if (this.selectKeys.length > 0) {
        // 父级分类的id
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        // 当前分类的权限 selectkeys的长度相当于当前分类权限的等级
        this.addCateForm.cat_level = this.selectKeys.length
      } else {
        // 没有父级分类重置pid和level数值
        this.addCateForm.cat_level = 0
        this.addCateForm.cat_pid = 0
      }
    },
    addCate() {
      //  表单预校验
      this.$refs.addCateFef.validate(async (valid) => {
        if (!valid) return
        // 预校验成功
        const { data: res } = await this.$http.post(
          'categories',
          this.addCateForm
        )
        if (res.meta.status !== 201) {
          return this.$message.error('创建失败')
        }
        this.$message.success('创建成功')
        // 调用getcatelist方法，重新渲染界面
        this.getCateList()
        this.adddialogVisible = false
      })
    }, // 重置表单数据
    addCateClose() {
      this.$refs.addCateFef.resetFields()
      this.selectKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    },
  },
}
// 在scope下只能给三方组件根组件添加样式，不能给三方组件的子组件添加样式
</script>
<style lang="less" scoped>
.tree-table {
  cursor: pointer;
  margin: 15px auto;
}
</style>
